package io.renren.modules.workjobs.service.impl;

import cn.hutool.json.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiniu.util.Json;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import io.renren.common.exception.RRException;
import io.renren.common.utils.*;
import io.renren.modules.common.dao.*;
import io.renren.modules.common.entity.*;
import io.renren.modules.common.viewdo.TreeOption;
import io.renren.modules.companys.dao.CompanyDao;
import io.renren.modules.companys.entity.CompanyEntity;
import io.renren.modules.resumes.dao.ResumeDao;
import io.renren.modules.resumes.entity.ResumeEntity;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.dao.UserBaseInfoDao;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.entity.UserBaseInfoEntity;
import io.renren.modules.workjobs.dao.DeliveryCreateUserPositionDao;
import io.renren.modules.workjobs.dao.UserPositionDeliverPersonalDao;
import io.renren.modules.workjobs.entity.DeliveryCreateUserPositionEntity;
import io.renren.modules.workjobs.entity.UserPositionDeliverPersonalEntity;
import io.renren.modules.workjobs.entity.dto.DeliverUserPositionDTO;
import io.renren.modules.workjobs.entity.dto.QueryCondition;
import io.renren.modules.workjobs.entity.dto.UserPositionDeliverDTO;
import io.renren.modules.workjobs.entity.dto.WorkJobQueryConditionDTO;
import io.renren.modules.workjobs.entity.vo.JobPositionCompanyInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.modules.workjobs.dao.WorkJobDao;
import io.renren.modules.workjobs.entity.WorkJobEntity;
import io.renren.modules.workjobs.service.WorkJobService;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.stylesheets.LinkStyle;


@Service("workJobService")
public class WorkJobServiceImpl extends ServiceImpl<WorkJobDao, WorkJobEntity> implements WorkJobService {

    @Autowired
    private TradeCategoryDao tradeCategoryDao;
    @Autowired
    private TradeClassifyDao tradeClassifyDao;
    @Autowired
    private TradeJobDao tradeJobDao;
    @Autowired
    private WorkJobDao workJobDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private DeliveryCreateUserPositionDao deliveryCreateUserPositionDao;
    @Autowired
    private ResumeDao resumeDao;

    @Autowired
    private UserBaseInfoDao userBaseInfoDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private ProvincesDao provincesDao;
    @Autowired
    private CitiesDao citiesDao;
    @Autowired
    private AreasDao areasDao;
    @Autowired
    private EducationDao educationDao;
    @Autowired
    private UserPositionDeliverPersonalDao userPositionDeliverPersonalDao;

    @Autowired
    private CompanyPropertyDao companyPropertyDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String jsonStr = (String) params.get("queryCondition");
        WorkJobQueryConditionDTO workJobQueryConditionDTO = Json.decode(jsonStr, WorkJobQueryConditionDTO.class);

        QueryWrapper<WorkJobEntity> workJobWrapper = new QueryWrapper<>();

        if (workJobQueryConditionDTO != null){

            if (workJobQueryConditionDTO.getJobName()!=null){
                workJobWrapper.like("job_name",workJobQueryConditionDTO.getJobName());
            }

            if (workJobQueryConditionDTO.getIsUrgency()!=null){
                workJobWrapper.eq("is_urgency",workJobQueryConditionDTO.getIsUrgency());
            }

            if (workJobQueryConditionDTO.getProvincesId()!=null &&
            workJobQueryConditionDTO.getCityesId()!=null &&
            workJobQueryConditionDTO.getAreasId()!=null){
                workJobWrapper.eq("provinces_id",workJobQueryConditionDTO.getProvincesId())
                        .eq("citys_id",workJobQueryConditionDTO.getCityesId())
                        .eq("areas_id",workJobQueryConditionDTO.getAreasId());
            }

            if (workJobQueryConditionDTO.getTradeId()!=null &&
            workJobQueryConditionDTO.getClassifyId()!=null &&
            workJobQueryConditionDTO.getJobTypeId()!=null){
                workJobWrapper.eq("trade_id",workJobQueryConditionDTO.getTradeId())
                        .eq("classify_id",workJobQueryConditionDTO.getClassifyId())
                        .eq("job_type_id",workJobQueryConditionDTO.getJobTypeId());
            }
        }

        IPage<WorkJobEntity> page = this.page(
                new Query<WorkJobEntity>().getPage(params),
                workJobWrapper
                //new QueryWrapper<WorkJobEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TreeOption> treeOptionCCJ() {

        List<TreeOption> topList = new ArrayList<>();

        List<TradeCategoryEntity> tradeCategoryEntities = tradeCategoryDao.selectList(null);
        List<TradeClassifyEntity> tradeClassifyEntities = tradeClassifyDao.selectList(null);
        List<TradeJobEntity> tradeJobEntities = tradeJobDao.selectList(null);

        topList = tradeCategoryEntities.stream().map(item -> new TreeOption(item.getTradeId(),item.getTradeName())).collect(Collectors.toList());

        for (TreeOption treeOption:topList){
            List<TradeClassifyEntity> collect = tradeClassifyEntities.stream().filter(item -> treeOption.getValue().equals(item.getTradeId())).collect(Collectors.toList());
            List<TreeOption> options = collect.stream().map(item -> new TreeOption(item.getClassifyId(), item.getClassifyName())).collect(Collectors.toList());
            treeOption.setChildren(options);
        }

        for (TreeOption treeOptionTop:topList){
            List<TreeOption> childrenTop = treeOptionTop.getChildren();
            for (TreeOption treeOptionMid : childrenTop){
                List<TradeJobEntity> collect = tradeJobEntities.stream().filter(item -> treeOptionMid.getValue().equals(item.getClassifyId())).collect(Collectors.toList());
                List<TreeOption> options = collect.stream().map(item -> new TreeOption(item.getJobTypeId(), item.getJobName())).collect(Collectors.toList());
                treeOptionMid.setChildren(options);
            }
        }
        return topList;
    }

    @Override
    public boolean updateWorkPublishStatus(WorkJobEntity workJobEntity) {
        boolean isUpdate = workJobDao.updatePublishStatus(workJobEntity.getId());
        return isUpdate;
    }

    @Override
    public R showList(String curent,String size) {

        //分页条目
        long currentPage = Long.parseLong(curent);
        long limit = Long.parseLong(size);

        IPage<WorkJobEntity> page = new Page();
        page.setCurrent(currentPage);
        page.setSize(limit);

        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.selectList(null);

        List<SysUserRoleEntity> companyRoles = sysUserRoleEntities.stream().filter(item -> RoleCode.COMPANY_ROLE.equals(item.getRoleId())).collect(Collectors.toList());
        List<Long> companyIds = companyRoles.stream().map(item -> item.getUserId()).collect(Collectors.toList());
        List<SysUserRoleEntity> headHuntimgRoles = sysUserRoleEntities.stream().filter(item -> RoleCode.SUB_ADMIN.equals(item.getRoleId())).collect(Collectors.toList());
        List<Long> headHuntingIds = headHuntimgRoles.stream().map(item -> item.getUserId()).collect(Collectors.toList());


        //不是急聘的项目 企业
        QueryWrapper<WorkJobEntity> wapper1 = new QueryWrapper<>();
        wapper1.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                .eq("is_urgency",0)
                .in("create_user_id",companyIds);



        workJobDao.selectPage(page, wapper1);
        PageUtils pageDataCompanyNotIsUrgency = new PageUtils(page);

        //是急聘类项目 企业
        QueryWrapper<WorkJobEntity> wapper2 = new QueryWrapper<>();
        wapper2.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                .eq("is_urgency",1)
                .in("create_user_id",companyIds);
        workJobDao.selectPage(page, wapper2);
        PageUtils pageDataCompanyIsUrgency = new PageUtils(page);



        //不是急聘的项目 猎头
        QueryWrapper<WorkJobEntity> wapper3 = new QueryWrapper<>();
        wapper3.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                .eq("is_urgency",0)
                .in("create_user_id",headHuntingIds);
        workJobDao.selectPage(page, wapper3);
        PageUtils pageDataHeadHuntingNotIsUrgency = new PageUtils(page);


        //急聘的项目 猎头
        QueryWrapper<WorkJobEntity> wapper4 = new QueryWrapper<>();
        wapper4.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                .eq("is_urgency",1)
                .in("create_user_id",headHuntingIds);
        workJobDao.selectPage(page, wapper4);
        PageUtils pageDataHeadHuntingIsUrgency = new PageUtils(page);



        return R.ok().put("pageDataCompanyNotIsUrgency",pageDataCompanyNotIsUrgency)
                .put("pageDataCompanyIsUrgency",pageDataCompanyIsUrgency)
                .put("pageDataHeadHuntingNotIsUrgency",pageDataHeadHuntingNotIsUrgency)
                .put("pageDataHeadHuntingIsUrgency",pageDataHeadHuntingIsUrgency);
    }

    @Override
    public R showListWorkInfo(String current, String size, QueryCondition queryCondition) {


        //0.准备好存储数据的容器
        List<JobPositionCompanyInfoVo> companyNotIsUrgenceList = new ArrayList<>();
        List<JobPositionCompanyInfoVo> companyIsUrgenceList = new ArrayList<>();
        List<JobPositionCompanyInfoVo> headHuntingNotIsUrgenceList = new ArrayList<>();
        List<JobPositionCompanyInfoVo> headHuntingIsUrgenceList = new ArrayList<>();

        //0.1 查询出来所有的企业信息
        List<CompanyEntity> companyEntityList = companyDao.selectList(null);

        //1.设置分页规则
        long currentPage = Long.parseLong(current);
        long limit = Long.parseLong(size);

        IPage<WorkJobEntity> page = new Page();
        page.setCurrent(currentPage);
        page.setSize(limit);

        //2.找出来那些人是企业人员，那些是猎头用户
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.selectList(null);
        List<SysUserRoleEntity> companyRoles = sysUserRoleEntities.stream().filter(item -> RoleCode.COMPANY_ROLE.equals(item.getRoleId())).collect(Collectors.toList());
        List<Long> companyIds = companyRoles.stream().map(item -> item.getUserId()).collect(Collectors.toList());

        List<SysUserRoleEntity> headHuntimgRoles = sysUserRoleEntities.stream().filter(item -> RoleCode.SUB_ADMIN.equals(item.getRoleId())).collect(Collectors.toList());
        List<Long> headHuntingIds = headHuntimgRoles.stream().map(item -> item.getUserId()).collect(Collectors.toList());

        //3.找出来是企业招聘的工作 非紧急项目 ,所有职位
        QueryWrapper<WorkJobEntity> wapper1 = new QueryWrapper<>();
        wapper1.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                //.eq("is_urgency",0)
                .in("create_user_id",companyIds);

                if (queryCondition.getProvincesId()!=null && (
                        queryCondition.getCityesId() == null
                        &&
                        queryCondition.getAreasId() ==null
                        )){
                    wapper1.eq("provinces_id",queryCondition.getProvincesId());
                }
                //根据地址进行筛选
                if (queryCondition.getProvincesId()!=null &&
                    queryCondition.getCityesId()!=null &&
                    queryCondition.getAreasId()!= null){
                    wapper1.eq("provinces_id",queryCondition.getProvincesId());
                    wapper1.eq("citys_id",queryCondition.getCityesId());
                    wapper1.eq("areas_id",queryCondition.getAreasId());
                }
                //根据行业进行筛选
                if (queryCondition.getTradeId()!=null &&
                        (
                            queryCondition.getClassifyId()==null &&
                            queryCondition.getJobTypeId() == null
                        )
                    ){
                    wapper1.eq("trade_id",queryCondition.getTradeId());
                }

                if (queryCondition.getTradeId() == null &&
                    queryCondition.getClassifyId() == null &&
                    queryCondition.getJobTypeId() !=null){
                    wapper1.eq("job_type_id",queryCondition.getJobTypeId());
                }

                if(queryCondition.getTradeId() !=null &&
                    queryCondition.getClassifyId() != null &&
                    queryCondition.getJobTypeId() == null){
                    wapper1.eq("trade_id",queryCondition.getTradeId());
                    wapper1.eq("classify_id",queryCondition.getClassifyId());
                }

                if (queryCondition.getTradeId()!=null&&
                    queryCondition.getClassifyId()!=null &&
                    queryCondition.getJobTypeId()!=null
                    ){
                    wapper1.eq("trade_id",queryCondition.getTradeId());
                    wapper1.eq("classify_id",queryCondition.getClassifyId());
                    wapper1.eq("job_type_id",queryCondition.getJobTypeId());
                }


                //根据薪资的区间范围进行搜索
                if (queryCondition.getStartSalary() != null && queryCondition.getEndSalary() !=null){
                    wapper1.ge("job_salary_start",queryCondition.getStartSalary());
                    wapper1.lt("job_salary_end",queryCondition.getEndSalary());
                }
                if (queryCondition.getEndSalary()!=null && queryCondition.getStartSalary()==null){
                    wapper1.lt("job_salary_start",queryCondition.getEndSalary());
                }
                if (queryCondition.getStartSalary()!=null && queryCondition.getEndSalary() ==null){
                    wapper1.ge("job_salary_start",queryCondition.getStartSalary());
                }
                if (queryCondition.getWorkJobName()!=null){
                    wapper1.like("job_name",queryCondition.getWorkJobName());
                }

        workJobDao.selectPage(page, wapper1);
        PageUtils pageDataCompanyNotIsUrgency = new PageUtils(page);

        for (WorkJobEntity workJobItem:(List<WorkJobEntity>)pageDataCompanyNotIsUrgency.getList()){
            JobPositionCompanyInfoVo jobPositionCompanyInfoVo = new JobPositionCompanyInfoVo();

            for (int i=0;i<companyEntityList.size();i++){
                if (companyEntityList.get(i).getCreateUserId().equals(workJobItem.getCreateUserId())){
                    jobPositionCompanyInfoVo.setCompanyId(companyEntityList.get(i).getId());
                    jobPositionCompanyInfoVo.setCompanyName(companyEntityList.get(i).getCompanyName());

                    QueryWrapper<CompanyPropertyEntity> wrapper = new QueryWrapper<CompanyPropertyEntity>();
                    wrapper.eq("id",companyEntityList.get(i).getCompanyPropertyId());
                    CompanyPropertyEntity companyPropertyEntity = companyPropertyDao.selectOne(wrapper);
                    jobPositionCompanyInfoVo.setCompanyNature(companyPropertyEntity.getCompanyProppertyName());
                }
            }

            jobPositionCompanyInfoVo.setWorkJobId(workJobItem.getId());
            jobPositionCompanyInfoVo.setWorkJobName(workJobItem.getJobName());
            jobPositionCompanyInfoVo.setSalaryStart(workJobItem.getJobSalaryStart());
            jobPositionCompanyInfoVo.setSalaryEnd(workJobItem.getJobSalaryEnd());

            QueryWrapper<ProvincesEntity> provincesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<CitiesEntity> citiesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<AreasEntity> areasEntityQueryWrapper = new QueryWrapper<>();

            provincesEntityQueryWrapper.eq("provinceid",workJobItem.getProvincesId());
            citiesEntityQueryWrapper.eq("cityid",workJobItem.getCitysId());
            areasEntityQueryWrapper.eq("areaid",workJobItem.getAreasId());

            ProvincesEntity provincesEntity = provincesDao.selectOne(provincesEntityQueryWrapper);
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesEntityQueryWrapper);
            AreasEntity areasEntity = areasDao.selectOne(areasEntityQueryWrapper);

            jobPositionCompanyInfoVo.setProviceName(provincesEntity.getProvincename());
            jobPositionCompanyInfoVo.setCityesName(citiesEntity.getCityname());
            jobPositionCompanyInfoVo.setAreasName(areasEntity.getAreaname());

            QueryWrapper<EducationEntity> educationEntityQueryWrapper = new QueryWrapper<>();
            educationEntityQueryWrapper.eq("id",workJobItem.getEducationId());
            EducationEntity educationEntity = educationDao.selectOne(educationEntityQueryWrapper);
            if (educationEntity !=null){
                jobPositionCompanyInfoVo.setEducationName(educationEntity.getEducationName());
            }

            long time = workJobItem.getCreateTime().getTime();
            long currentTimeMillis = System.currentTimeMillis();

            long deffTimeMillis = currentTimeMillis - time;
            long dayLong = deffTimeMillis / (60*60*24*1000);
            jobPositionCompanyInfoVo.setDayLong(dayLong);

            QueryWrapper<TradeJobEntity> tradeJobEntityQueryWrapper = new QueryWrapper<>();
            tradeJobEntityQueryWrapper.eq("job_type_id",workJobItem.getJobTypeId());
            TradeJobEntity tradeJobEntity = tradeJobDao.selectOne(tradeJobEntityQueryWrapper);
            //jobPositionCompanyInfoVo.setWorkJobTypeName(tradeJobEntity.getJobName());

            companyNotIsUrgenceList.add(jobPositionCompanyInfoVo);
        }

        //3.找出来是企业招聘的工作 紧急招聘职位
        QueryWrapper<WorkJobEntity> wapper2 = new QueryWrapper<>();
        wapper2.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                .eq("is_urgency",1)
                .in("create_user_id",companyIds);
        workJobDao.selectPage(page, wapper2);
        PageUtils pageDataCompanyIsUrgency = new PageUtils(page);

        for (WorkJobEntity workJobItem:(List<WorkJobEntity>)pageDataCompanyIsUrgency.getList()){
            JobPositionCompanyInfoVo jobPositionCompanyInfoVo = new JobPositionCompanyInfoVo();

            for (int i=0;i<companyEntityList.size();i++){
                if (companyEntityList.get(i).getCreateUserId().equals(workJobItem.getCreateUserId())){
                    jobPositionCompanyInfoVo.setCompanyId(companyEntityList.get(i).getId());
                    jobPositionCompanyInfoVo.setCompanyName(companyEntityList.get(i).getCompanyName());

                    QueryWrapper<CompanyPropertyEntity> wrapper = new QueryWrapper<CompanyPropertyEntity>();
                    wrapper.eq("id",companyEntityList.get(i).getCompanyPropertyId());
                    CompanyPropertyEntity companyPropertyEntity = companyPropertyDao.selectOne(wrapper);
                    jobPositionCompanyInfoVo.setCompanyNature(companyPropertyEntity.getCompanyProppertyName());
                }
            }

            jobPositionCompanyInfoVo.setWorkJobId(workJobItem.getId());
            jobPositionCompanyInfoVo.setWorkJobName(workJobItem.getJobName());
            jobPositionCompanyInfoVo.setSalaryStart(workJobItem.getJobSalaryStart());
            jobPositionCompanyInfoVo.setSalaryEnd(workJobItem.getJobSalaryEnd());

            QueryWrapper<ProvincesEntity> provincesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<CitiesEntity> citiesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<AreasEntity> areasEntityQueryWrapper = new QueryWrapper<>();

            provincesEntityQueryWrapper.eq("provinceid",workJobItem.getProvincesId());
            citiesEntityQueryWrapper.eq("cityid",workJobItem.getCitysId());
            areasEntityQueryWrapper.eq("areaid",workJobItem.getAreasId());

            ProvincesEntity provincesEntity = provincesDao.selectOne(provincesEntityQueryWrapper);
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesEntityQueryWrapper);
            AreasEntity areasEntity = areasDao.selectOne(areasEntityQueryWrapper);

            jobPositionCompanyInfoVo.setProviceName(provincesEntity.getProvincename());
            jobPositionCompanyInfoVo.setCityesName(citiesEntity.getCityname());
            jobPositionCompanyInfoVo.setAreasName(areasEntity.getAreaname());

            QueryWrapper<EducationEntity> educationEntityQueryWrapper = new QueryWrapper<>();
            educationEntityQueryWrapper.eq("id",workJobItem.getEducationId());
            EducationEntity educationEntity = educationDao.selectOne(educationEntityQueryWrapper);
            if (educationEntity !=null){
                jobPositionCompanyInfoVo.setEducationName(educationEntity.getEducationName());
            }

            long time = workJobItem.getCreateTime().getTime();
            long currentTimeMillis = System.currentTimeMillis();

            long deffTimeMillis = currentTimeMillis - time;
            long dayLong = deffTimeMillis / (60*60*24*1000);
            jobPositionCompanyInfoVo.setDayLong(dayLong);

            QueryWrapper<TradeJobEntity> tradeJobEntityQueryWrapper = new QueryWrapper<>();
            tradeJobEntityQueryWrapper.eq("job_type_id",workJobItem.getJobTypeId());
            TradeJobEntity tradeJobEntity = tradeJobDao.selectOne(tradeJobEntityQueryWrapper);
            //jobPositionCompanyInfoVo.setWorkJobTypeName(tradeJobEntity.getJobName());

            companyIsUrgenceList.add(jobPositionCompanyInfoVo);
        }



        //3.不是急聘的项目 猎头 ， 所有猎头发布的职位
        QueryWrapper<WorkJobEntity> wapper3 = new QueryWrapper<>();
        wapper3.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                //.eq("is_urgency",0)
                .in("create_user_id",headHuntingIds);



        if (queryCondition.getProvincesId()!=null && (
                queryCondition.getCityesId() == null
                        &&
                        queryCondition.getAreasId() ==null
        )){
            wapper3.eq("provinces_id",queryCondition.getProvincesId());
        }
        //根据地址进行筛选
        if (queryCondition.getProvincesId()!=null &&
                queryCondition.getCityesId()!=null &&
                queryCondition.getAreasId()!= null){
            wapper3.eq("provinces_id",queryCondition.getProvincesId());
            wapper3.eq("citys_id",queryCondition.getCityesId());
            wapper3.eq("areas_id",queryCondition.getAreasId());
        }
        //根据行业进行筛选
        if (queryCondition.getTradeId()!=null &&
                (
                        queryCondition.getClassifyId()==null &&
                        queryCondition.getJobTypeId() == null
                )
        ){
            wapper3.eq("trade_id",queryCondition.getTradeId());
        }

        if (queryCondition.getTradeId() == null &&
                queryCondition.getClassifyId() == null &&
                queryCondition.getJobTypeId() !=null){
            wapper3.eq("job_type_id",queryCondition.getJobTypeId());
        }

        if(queryCondition.getTradeId() !=null &&
                queryCondition.getClassifyId() != null &&
                queryCondition.getJobTypeId() == null){
            wapper1.eq("trade_id",queryCondition.getTradeId());
            wapper1.eq("classify_id",queryCondition.getClassifyId());
        }

        if (queryCondition.getTradeId()!=null&&
                queryCondition.getClassifyId()!=null &&
                queryCondition.getJobTypeId()!=null
        ){
            wapper3.eq("trade_id",queryCondition.getTradeId());
            wapper3.eq("classify_id",queryCondition.getClassifyId());
            wapper3.eq("job_type_id",queryCondition.getJobTypeId());
        }


        //根据薪资的区间范围进行搜索
        if (queryCondition.getStartSalary() != null && queryCondition.getEndSalary() !=null){
            wapper3.ge("job_salary_start",queryCondition.getStartSalary());
            wapper3.lt("job_salary_end",queryCondition.getEndSalary());
        }
        if (queryCondition.getEndSalary()!=null && queryCondition.getStartSalary()==null){
            wapper3.lt("job_salary_start",queryCondition.getEndSalary());
        }
        if (queryCondition.getStartSalary()!=null && queryCondition.getEndSalary() ==null){
            wapper3.ge("job_salary_start",queryCondition.getStartSalary());
        }

        if (queryCondition.getWorkJobName()!=null){
            wapper3.like("job_name",queryCondition.getWorkJobName());
        }

        workJobDao.selectPage(page, wapper3);
        PageUtils pageDataHeadHuntingNotIsUrgency = new PageUtils(page);

        for (WorkJobEntity workJobItem:(List<WorkJobEntity>)pageDataHeadHuntingNotIsUrgency.getList()){
            JobPositionCompanyInfoVo jobPositionCompanyInfoVo = new JobPositionCompanyInfoVo();

            for (int i=0;i<companyEntityList.size();i++){
                if (companyEntityList.get(i).getCreateUserId().equals(workJobItem.getCreateUserId())){
                    jobPositionCompanyInfoVo.setCompanyId(companyEntityList.get(i).getId());
                    jobPositionCompanyInfoVo.setCompanyName(companyEntityList.get(i).getCompanyName());

                    QueryWrapper<CompanyPropertyEntity> wrapper = new QueryWrapper<CompanyPropertyEntity>();
                    wrapper.eq("id",companyEntityList.get(i).getCompanyPropertyId());
                    CompanyPropertyEntity companyPropertyEntity = companyPropertyDao.selectOne(wrapper);
                    jobPositionCompanyInfoVo.setCompanyNature(companyPropertyEntity.getCompanyProppertyName());
                }
            }

            jobPositionCompanyInfoVo.setWorkJobId(workJobItem.getId());
            jobPositionCompanyInfoVo.setWorkJobName(workJobItem.getJobName());
            jobPositionCompanyInfoVo.setSalaryStart(workJobItem.getJobSalaryStart());
            jobPositionCompanyInfoVo.setSalaryEnd(workJobItem.getJobSalaryEnd());

            QueryWrapper<ProvincesEntity> provincesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<CitiesEntity> citiesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<AreasEntity> areasEntityQueryWrapper = new QueryWrapper<>();

            provincesEntityQueryWrapper.eq("provinceid",workJobItem.getProvincesId());
            citiesEntityQueryWrapper.eq("cityid",workJobItem.getCitysId());
            areasEntityQueryWrapper.eq("areaid",workJobItem.getAreasId());

            ProvincesEntity provincesEntity = provincesDao.selectOne(provincesEntityQueryWrapper);
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesEntityQueryWrapper);
            AreasEntity areasEntity = areasDao.selectOne(areasEntityQueryWrapper);

            jobPositionCompanyInfoVo.setProviceName(provincesEntity.getProvincename());
            jobPositionCompanyInfoVo.setCityesName(citiesEntity.getCityname());
            jobPositionCompanyInfoVo.setAreasName(areasEntity.getAreaname());

            QueryWrapper<EducationEntity> educationEntityQueryWrapper = new QueryWrapper<>();
            educationEntityQueryWrapper.eq("id",workJobItem.getEducationId());
            EducationEntity educationEntity = educationDao.selectOne(educationEntityQueryWrapper);
            if (educationEntity !=null){
                jobPositionCompanyInfoVo.setEducationName(educationEntity.getEducationName());
            }

            long time = workJobItem.getCreateTime().getTime();
            long currentTimeMillis = System.currentTimeMillis();

            long deffTimeMillis = currentTimeMillis - time;
            long dayLong = deffTimeMillis / (60*60*24*1000);
            jobPositionCompanyInfoVo.setDayLong(dayLong);

            QueryWrapper<TradeJobEntity> tradeJobEntityQueryWrapper = new QueryWrapper<>();
            tradeJobEntityQueryWrapper.eq("job_type_id",workJobItem.getJobTypeId());
            TradeJobEntity tradeJobEntity = tradeJobDao.selectOne(tradeJobEntityQueryWrapper);
            //jobPositionCompanyInfoVo.setWorkJobTypeName(tradeJobEntity.getJobName());

            headHuntingNotIsUrgenceList.add(jobPositionCompanyInfoVo);
        }

        //3.急聘的项目 猎头
        QueryWrapper<WorkJobEntity> wapper4 = new QueryWrapper<>();
        wapper4.eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
                .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_is_pass",1)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING)
                .eq("is_urgency",1)
                .in("create_user_id",headHuntingIds);
        workJobDao.selectPage(page, wapper4);
        PageUtils pageDataHeadHuntingIsUrgency = new PageUtils(page);

        for (WorkJobEntity workJobItem:(List<WorkJobEntity>)pageDataHeadHuntingIsUrgency.getList()){
            JobPositionCompanyInfoVo jobPositionCompanyInfoVo = new JobPositionCompanyInfoVo();

            for (int i=0;i<companyEntityList.size();i++){
                if (companyEntityList.get(i).getCreateUserId().equals(workJobItem.getCreateUserId())){
                    jobPositionCompanyInfoVo.setCompanyId(companyEntityList.get(i).getId());
                    jobPositionCompanyInfoVo.setCompanyName(companyEntityList.get(i).getCompanyName());

                    QueryWrapper<CompanyPropertyEntity> wrapper = new QueryWrapper<CompanyPropertyEntity>();
                    wrapper.eq("id",companyEntityList.get(i).getCompanyPropertyId());
                    CompanyPropertyEntity companyPropertyEntity = companyPropertyDao.selectOne(wrapper);
                    jobPositionCompanyInfoVo.setCompanyNature(companyPropertyEntity.getCompanyProppertyName());
                }
            }

            jobPositionCompanyInfoVo.setWorkJobId(workJobItem.getId());
            jobPositionCompanyInfoVo.setWorkJobName(workJobItem.getJobName());
            jobPositionCompanyInfoVo.setSalaryStart(workJobItem.getJobSalaryStart());
            jobPositionCompanyInfoVo.setSalaryEnd(workJobItem.getJobSalaryEnd());

            QueryWrapper<ProvincesEntity> provincesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<CitiesEntity> citiesEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<AreasEntity> areasEntityQueryWrapper = new QueryWrapper<>();

            provincesEntityQueryWrapper.eq("provinceid",workJobItem.getProvincesId());
            citiesEntityQueryWrapper.eq("cityid",workJobItem.getCitysId());
            areasEntityQueryWrapper.eq("areaid",workJobItem.getAreasId());

            ProvincesEntity provincesEntity = provincesDao.selectOne(provincesEntityQueryWrapper);
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesEntityQueryWrapper);
            AreasEntity areasEntity = areasDao.selectOne(areasEntityQueryWrapper);

            jobPositionCompanyInfoVo.setProviceName(provincesEntity.getProvincename());
            jobPositionCompanyInfoVo.setCityesName(citiesEntity.getCityname());
            jobPositionCompanyInfoVo.setAreasName(areasEntity.getAreaname());

            QueryWrapper<EducationEntity> educationEntityQueryWrapper = new QueryWrapper<>();
            educationEntityQueryWrapper.eq("id",workJobItem.getEducationId());
            EducationEntity educationEntity = educationDao.selectOne(educationEntityQueryWrapper);
            if (educationEntity !=null){
                jobPositionCompanyInfoVo.setEducationName(educationEntity.getEducationName());
            }

            long time = workJobItem.getCreateTime().getTime();
            long currentTimeMillis = System.currentTimeMillis();

            long deffTimeMillis = currentTimeMillis - time;
            long dayLong = deffTimeMillis / (60*60*24*1000);
            jobPositionCompanyInfoVo.setDayLong(dayLong);

            QueryWrapper<TradeJobEntity> tradeJobEntityQueryWrapper = new QueryWrapper<>();
            tradeJobEntityQueryWrapper.eq("job_type_id",workJobItem.getJobTypeId());
            TradeJobEntity tradeJobEntity = tradeJobDao.selectOne(tradeJobEntityQueryWrapper);
            //jobPositionCompanyInfoVo.setWorkJobTypeName(tradeJobEntity.getJobName());

            headHuntingIsUrgenceList.add(jobPositionCompanyInfoVo);
        }

        return R.ok().
            put("pageDataCompanyNotIsUrgency",companyNotIsUrgenceList).
            put("pageDataCompanyNotIsUrgencyTotal",pageDataCompanyNotIsUrgency.getTotalCount()).
            put("pageDataCompanyIsUrgency",companyIsUrgenceList).
            put("pageDataCompanyIsUrgencyTotal",pageDataCompanyIsUrgency.getTotalCount()).
            put("pageDataHeadHuntingNotIsUrgency",headHuntingNotIsUrgenceList).
            put("pageDataHeadHuntingNotIsUrgencyTotal",pageDataHeadHuntingNotIsUrgency.getTotalCount()).
            put("pageDataHeadHuntingIsUrgency",headHuntingIsUrgenceList).
            put("pageDataHeadHuntingIsUrgencyTotal",pageDataHeadHuntingIsUrgency.getTotalCount());
    }

    @Override
    public boolean deliver(String workJobId, String userId) {
        QueryWrapper<UserPositionDeliverPersonalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId)
                .eq("position_id",workJobId);
        UserPositionDeliverPersonalEntity userPositionDeliverPersonalEntity = userPositionDeliverPersonalDao.selectOne(wrapper);
        if (userPositionDeliverPersonalEntity!=null){
            throw new RRException("您已经投递过次岗位，请不要重复投递");
        }
        UserPositionDeliverPersonalEntity userPositionEntity = new UserPositionDeliverPersonalEntity();
        userPositionEntity.setUserId(Long.parseLong(userId));
        userPositionEntity.setPositionId(workJobId);
        int isInsert = userPositionDeliverPersonalDao.insert(userPositionEntity);
        if (isInsert>0){
            return true;
        }
        return false;
    }

    @Override
    public R IPageList(String userId, String current, String size) {

        QueryWrapper<UserPositionDeliverPersonalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserPositionDeliverPersonalEntity> userPositionDeliverEntities = userPositionDeliverPersonalDao.selectList(wrapper);

        //如果我这没有投递的岗位，直接返回
        if(userPositionDeliverEntities.size() == 0){
            return R.ok();
        }

        //要返回的数据信息
        List<UserPositionDeliverDTO> resultDTO = new ArrayList<>();

        for (UserPositionDeliverPersonalEntity deliverPersonalEntity:userPositionDeliverEntities){
            UserPositionDeliverDTO userPositionDeliverDTO = new UserPositionDeliverDTO();

            QueryWrapper<WorkJobEntity> workJobWrapper = new QueryWrapper<>();
            workJobWrapper.eq("id",deliverPersonalEntity.getPositionId());
            WorkJobEntity workJobEntity = workJobDao.selectOne(workJobWrapper);

            userPositionDeliverDTO.setJobName(workJobEntity.getJobName());
            userPositionDeliverDTO.setJobContact(workJobEntity.getJobContact());
            userPositionDeliverDTO.setJobPhone(workJobEntity.getJobPhone());
            userPositionDeliverDTO.setJobDeptement(workJobEntity.getJobDeptement());
            userPositionDeliverDTO.setJobWechat(workJobEntity.getJobWechat());
            userPositionDeliverDTO.setCreateTime(deliverPersonalEntity.getCreateTime());
            userPositionDeliverDTO.setFeedback(deliverPersonalEntity.getFeedback());
            resultDTO.add(userPositionDeliverDTO);
        }
        IPage<UserPositionDeliverDTO> page = new Page<>();
        long currentPage = Long.parseLong(current);
        long limit = Long.parseLong(size);

        page.setCurrent(currentPage);
        page.setSize(limit);


        page.setRecords(resultDTO);
        page.setTotal(resultDTO.size());

        PageUtils pageUtils = new PageUtils(page);
        return R.ok().put("limitPage",pageUtils);
    }

    @Override
    public R queryDeliverPosition(String userId,String current, String size) {

        long currentPage = Long.parseLong(current);
        long limit = Long.parseLong(size);


        IPage<DeliveryCreateUserPositionEntity> page = new Page<>();

        page.setCurrent(currentPage);
        page.setSize(limit);

        //要返回的data
        List<DeliverUserPositionDTO> listData = new ArrayList<>();

        //首先进行分页
        QueryWrapper<DeliveryCreateUserPositionEntity> wrapper = new QueryWrapper();
        wrapper.eq("user_create_position_id",userId);
        deliveryCreateUserPositionDao.selectPage(page,wrapper);
        PageUtils pageUtils = new PageUtils(page);

        List<DeliveryCreateUserPositionEntity> lists= (List<DeliveryCreateUserPositionEntity>) pageUtils.getList();
        int totalCount = pageUtils.getTotalCount();
        //没有查到投递信息
        if (lists.size() == 0){
            return R.ok().put("listdata",listData).put("totalCount",0);
        }

        for (DeliveryCreateUserPositionEntity deliveryEntity:lists){
            DeliverUserPositionDTO entity = new DeliverUserPositionDTO();

            QueryWrapper<WorkJobEntity> workJobEntityQueryWrapper= new QueryWrapper<>();
            QueryWrapper<UserBaseInfoEntity> baseInfoEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<ResumeEntity> resumeEntityQueryWrapper = new QueryWrapper<>();


            String positionId = deliveryEntity.getPositionId();             //投递者职位的Id
            Long userDeliveryId = deliveryEntity.getUserDeliveryId();       //投递者的Id

            workJobEntityQueryWrapper.eq("id",positionId);
            baseInfoEntityQueryWrapper.eq("user_info_id",userDeliveryId);
            resumeEntityQueryWrapper.eq("user_id",userDeliveryId);

            WorkJobEntity workJobEntity = workJobDao.selectOne(workJobEntityQueryWrapper);
            UserBaseInfoEntity userBaseInfoEntity = userBaseInfoDao.selectOne(baseInfoEntityQueryWrapper);
            ResumeEntity resumeEntity = resumeDao.selectOne(resumeEntityQueryWrapper);

            entity.setDeliveryId(deliveryEntity.getUserDeliveryId());
            entity.setCreatePositionUserId(deliveryEntity.getUserCreatePositionId());
            entity.setDeliveryName(userBaseInfoEntity.getName());
            entity.setPhone(userBaseInfoEntity.getPhone());
            entity.setJobName(workJobEntity.getJobName());
            entity.setJobDeptement(workJobEntity.getJobDeptement());
            entity.setResumePath(resumeEntity.getResumeSavePath());
            entity.setCreateTime(deliveryEntity.getCreateTime());
            entity.setFeedback(deliveryEntity.getFeedback());
            entity.setPositionId(deliveryEntity.getPositionId());
            listData.add(entity);
        }
        return R.ok().put("listdata",listData).put("totalCount",totalCount);
    }


    @Override
    @Transactional
    public R fitOrNotFit(String deliveryId,String positionId,Integer feedback) {
        QueryWrapper<DeliveryCreateUserPositionEntity> deliveryCreateWrapper = new QueryWrapper<>();
        deliveryCreateWrapper.eq("user_delivery_id",Long.parseLong(deliveryId));
        deliveryCreateWrapper.eq("position_id",positionId);
        DeliveryCreateUserPositionEntity deliveryCreateUserPositionEntity = deliveryCreateUserPositionDao.selectOne(deliveryCreateWrapper);

        QueryWrapper<UserPositionDeliverPersonalEntity> userPositionWapper = new QueryWrapper<>();
        userPositionWapper.eq("user_id",deliveryId);
        userPositionWapper.eq("position_id",positionId);
        UserPositionDeliverPersonalEntity deliverPersonalEntity = userPositionDeliverPersonalDao.selectOne(userPositionWapper);

        if (feedback.equals(1)){
            deliveryCreateUserPositionEntity.setFeedback(1);
            deliverPersonalEntity.setFeedback(1);
            deliveryCreateUserPositionDao.updateById(deliveryCreateUserPositionEntity);
            userPositionDeliverPersonalDao.updateById(deliverPersonalEntity);
        }

        if (feedback.equals(2)){
            deliveryCreateUserPositionEntity.setFeedback(2);
            deliverPersonalEntity.setFeedback(2);
            deliveryCreateUserPositionDao.updateById(deliveryCreateUserPositionEntity);
            userPositionDeliverPersonalDao.updateById(deliverPersonalEntity);
        }
        return R.ok();
    }

/*    private R fitOrNotFitCommon(DeliveryCreateUserPositionEntity deliveryCreateUserPositionEntity,
                                UserPositionDeliverPersonalEntity deliverPersonalEntity,
                                Integer feedback){
        if (feedback.equals(1)){
            deliveryCreateUserPositionEntity.setFeedback(2);
            deliverPersonalEntity.setFeedback(2);
            deliveryCreateUserPositionDao.updateById(deliveryCreateUserPositionEntity);
            userPositionDeliverPersonalDao.updateById(deliverPersonalEntity);
        }

        if (feedback.equals(2)){
            deliveryCreateUserPositionEntity.setFeedback(1);
            deliverPersonalEntity.setFeedback(1);
            deliveryCreateUserPositionDao.updateById(deliveryCreateUserPositionEntity);
            userPositionDeliverPersonalDao.updateById(deliverPersonalEntity);
        }

        return R.ok();
    }*/


}